過濾從 Wagtail 核心頁面導入的多個模型的自定義字段 (Filter on custom field across multiple models that import from Wagtail core Page)


問題描述

過濾從 Wagtail 核心頁面導入的多個模型的自定義字段 (Filter on custom field across multiple models that import from Wagtail core Page)

我有兩個自定義 Page 模型共享一個共同的字段,例如:

class CustomPageModelOne(Page):
    custom_field = models.IntegerField()
    ...

class CustomPageModelTwo(Page):
    custom_field = models.IntegerField()
    ...

理想情況下,我需要跨兩種自定義類型運行單個過濾器頁面模型。Wagtail 文檔說我可以使用 exact_type 方法來指定從核心 Page 繼承的多個模型,所以我正在嘗試以下一些變體:

Page.objects.exact_type(CustomPageModelOne, CustomPageModelTwo).filter(custom_field=123)

但是,當我嘗試過濾使用這兩種模型的任何 QuerySet 時,我收到一個錯誤:

django.core.exceptions.FieldError: Cannot resolve keyword 'custom_field' into field。

如何跨多個共享一個字段的 Wagtail 自定義頁面模型進行查詢?

注意:


參考解法

方法 1:

As you do Page.objects... you can only filter on fields of the Page model and subclasses of Page

To filter specifically on fields of your CustomPageModelOne, you would have to use CustomPageModel.objects... where that model has that field and both your custom page models are subclasses from

方法 2:

Apparently Page.objects.exact_type is only returning a queryset based upon Page, which makes sense because exact_type has no way of knowing what fields would occur on models descended from Page. I would suggest the following as an alternative approach if re‑architecting your models is not an option:

from itertools import chain

model_one_results = CustomPageModelOne.objects.filter(custom_field=123)
model_two_results = CustomPageModelTwo.objects.filter(custom_field=123)
all_results = list(chain(model_one_results, model_two_results))

(by Brylie Christopher OxleyVincentDan Swain)

參考文件

  1. Filter on custom field across multiple models that import from Wagtail core Page (CC BY‑SA 2.5/3.0/4.0)

#wagtail #Django






相關問題

Wagtail Cms 是否支持 Google 登錄和用戶登錄添加會話 (Does Wagtail Cms support Google login and user login add session to)

Wagtail Django-form編輯現有對象 (Wagtail Django-form edit existing object)

如何將 Wagtail 'admin' 菜單添加到自定義模板? (How to add Wagtail 'admin' menu to custom templates?)

django.db.utils.OperationalError:外鍵不匹配 - “project_projectpage”引用“auth_user” (django.db.utils.OperationalError: foreign key mismatch - "project_projectpage" referencing "auth_user")

如何使用 Wagtail 鉤子在 Wagtail 中生成自定義鏈接 (How to generate a custom link in Wagtail using Wagtail hooks)

Wagtail:如何設置單元測試以進行簡單的頁面編輯? (Wagtail: How to setup up unittest for simple page edit?)

如何修復錯誤“str”對像沒有屬性“relative_url” (How to fix error 'str' object has no attribute 'relative_url')

如何將帖子從 Wordpress 導入 Wagtail 2(Draftail 編輯器),包括圖像? (How to import posts from Wordpress to Wagtail 2 (Draftail editor) including images?)

如何用外鍵鏈接兩種形式(wagtail 形式和 django 形式)? (How to link two forms (wagtail form and django form) with a foreign key?)

為什麼 RichText 不能在 wagtail 管理員中為帖子工作?這是發生的事情的類型:<h2>嘗試 post.content|richtext</h2> (Why is RichText not working in wagtail admin for posts? This is the type of thing that happens: <h2>Trying post.content|richtext</h2>)

Windows 10 上 wagtail 的客戶端文件夾在哪裡 (Where is the client folder of wagtail on windows 10)

過濾從 Wagtail 核心頁面導入的多個模型的自定義字段 (Filter on custom field across multiple models that import from Wagtail core Page)







留言討論